package com.yanqu.road.server.logic.activity.condition.season;

import afu.org.checkerframework.checker.oigj.qual.O;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.snowgarden.ActivityMallDayBuyMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

public class SeasonServerMallBuyTaskCondition extends BaseActivityCondition {
    public SeasonServerMallBuyTaskCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        int mallKey = paramList.get(0).intValue();
        MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityConditionInfo.getActivityId(), mallKey);
        if (mallInfo == null) {
            return;
        }

        int whichDay = ActivityHelper.getWhichDayAfterBeginTime(activityInfo);
        long count = ActivityMallDayBuyMgr.getActivityMallDayBuyTotalCount(activityInfo.getActivityId(), whichDay, mallInfo.getMallId());

        int oldDay = 0;
        if (!StringUtils.isNullOrEmpty(getParam())) {
            oldDay = Integer.parseInt(getParam());
        }
        setParam(String.valueOf(whichDay));

        if (oldDay != whichDay) {
            setGetReward(false);// 重置领奖的
            setValue(BigInteger.valueOf(count));
        } else {
            if (getValue().compareTo(BigInteger.valueOf(count)) < 0) {
                setValue(BigInteger.valueOf(count));
            }
        }
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        BigInteger valueNeed = paramList.get(3);
        int mallKey = paramList.get(0).intValue();
        MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityConditionInfo.getActivityId(), mallKey);
        if (mallInfo == null) {
            return false;
        }

        if (valueNeed.compareTo(getValue()) <= 0 && ActivityMallDayBuyMgr.isUserTodayBuyMall(activityInfo.getActivityId(), player.getUserId(), mallInfo.getMallId())) {
            return true;
        }

        return false;
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.SeasonServerMallBuyTask.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        //这个领取后还要保留监听，因为会每天重置
//        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.SeasonServerMallBuyTask.getValue(), handle);
    }

    @Override
    public String getReward() {
        String reward = super.getReward();
        //领奖后要通知MallDayBuyActivity, 防止重复补发邮件
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(activityConditionInfo.getConditionId());
        if (userActivityConditionData != null && userActivityConditionData.isGetReward()) {
            List<BigInteger> paramList = getActivityConditionInfo().getParamList();
            BigInteger valueNeed = paramList.get(3);
            int mallKey = paramList.get(0).intValue();
            MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityConditionInfo.getActivityId(), mallKey);
            if (mallInfo != null) {
                ActivityMallDayBuyMgr.updateGetConditionReward(activityInfo.getActivityId(), player.getUserId(), mallInfo.getMallId(), userActivityConditionData.getConditionId());
            }
        }


        return reward;
    }
}
